<?php


namespace dsp\factory;


use kernel\BaseMain;
use dsp\factory\abstracts\FactoryProducer;
use dsp\factory\functions\AppleFactory;
use dsp\factory\functions\XiaoMiFactory;

/**
 * 测试实例
 * 内容来源于：https://www.cnblogs.com/it-abel/p/11031845.html
 */
class Main extends BaseMain
{
    public function execute($choice = 'all')
    {
        switch ($choice) {
            case 'simple':
                $this->simpleFactory();
                break;
            case 'func':
            case 'function':
                $this->functionFactory();
                break;
            case 'abstract':
                $this->abstractFactory();
                break;
            case 'all':
                $this->simpleFactory();
                echo PHP_EOL;
                $this->functionFactory();
                echo PHP_EOL;
                $this->abstractFactory();
                echo PHP_EOL;
                break;
        }
    }

    /** 测试简单工厂模式 */
    public function simpleFactory()
    {
        echo "=================== 测试简单工厂模式 " . PHP_EOL;
        $factory = new \dsp\factory\simple\SimpleFactory();
        $productA = $factory->createProduct('productA');
        $productB = $factory->createProduct('productB');

        echo "---- productA 价格显示" . PHP_EOL;
        echo $productA->getPrice(40) . PHP_EOL;
        echo "---- productA 价格显示" . PHP_EOL;
        echo $productB->getPrice(40) . PHP_EOL;
    }

    /** 测试工厂方法模式 */
    public function functionFactory()
    {
        echo "=================== 测试工厂方法模式 " . PHP_EOL;
        echo "---- 创建苹果工厂" . PHP_EOL;
        $phoneFactory = new AppleFactory();
        echo "---- 生产iphone" . PHP_EOL;
        $phone = $phoneFactory->create();
        $phone->open();
        echo PHP_EOL;

        echo "---- 创建小米工厂" . PHP_EOL;
        $phoneFactory = new XiaoMiFactory();
        echo "---- 生产小米手机" . PHP_EOL;
        $phone = $phoneFactory->create();
        $phone->open();
        echo PHP_EOL;
    }

    public function abstractFactory()
    {
        echo "=================== 测试抽象工厂模式 " . PHP_EOL;
        echo "---- 获取形状、颜色工厂" . PHP_EOL;
        $shapeFactory = FactoryProducer::getFactory(FactoryProducer::SHAPE);
        $colorFactory = FactoryProducer::getFactory(FactoryProducer::COLOR);
        $shape = $shapeFactory->getShape('circle');
        $shape->draw();
        $color = $colorFactory->getColor('red');
        $color->fill();

        echo PHP_EOL;
        $shape = $shapeFactory->getShape('rectangle');
        $shape->draw();
        $color = $colorFactory->getColor('blue');
        $color->fill();
    }

}